package com.baizhi.service.impl;

import com.baizhi.dao.CmfzAdminDao;
import com.baizhi.entity.CmfzAdmin;
import com.baizhi.entity.CmfzBanner;
import com.baizhi.service.AdminService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.shiro.crypto.hash.Md5Hash;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.UUID;

@Service
public class AdminServiceImpl implements AdminService {

    @Resource
    private CmfzAdminDao adminDAO;

    @Override
    public boolean login(String username, String pwd) {
        boolean flag=true;
        try {
            QueryWrapper<CmfzAdmin> queryWrapper = new QueryWrapper<>();
            CmfzAdmin admin = adminDAO.selectOne(queryWrapper);
            if(admin == null) {
                flag = false;
            }else if(!admin.getPassword().equals(pwd)) {
                flag = false;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return flag;
    }

    @Override
    public Page<CmfzAdmin> selectAll(Integer page, Integer limit) {
        //1.创建一个分页对象 封装分页数据
        Page<CmfzAdmin> adminPage = new Page<>(page, limit);
        //2.使用分页查询的方法
        Page<CmfzAdmin> cmfzAdminPage = adminDAO.selectPage(adminPage, null);

        return cmfzAdminPage;
    }

    @Override
    public void insert(CmfzAdmin cmfzAdmin) {

        try {
            //        1.获取随机盐值
            String salt = UUID.randomUUID().toString().substring(0,4);
            System.out.println("随机盐值"+salt);
//        2.通过shiro加密工具类 对密码进行加密处理  加盐+散列
            Md5Hash md5Hash = new Md5Hash(cmfzAdmin.getPassword(), salt, 1024);
            String result=md5Hash.toHex();


//        3.将结果存储在数据库
            cmfzAdmin.setSalt(salt);
            cmfzAdmin.setPassword(result);

//        添加数据库


            adminDAO.insert(cmfzAdmin);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}
